<template>
  <div>
    <van-nav-bar
      class="result-contanner"
      fixed
      placeholder
      :title="`${$route.query.keyword}的搜索结果`"
      left-text="返回"
      left-arrow
      @click-left="$router.back()"
    />
    <van-list
      class="search-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id.toString()"
        :title="item.title"
        @click="$router.push({ path: '/article', query: { id: item.art_id } })"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearchList } from "./home.js";
export default {
  name: "searchResult",
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    };
  },
  methods: {
    async onLoad() {
      // 异步更新数据

      let res = await getSearchList({
        page: this.page,
        per_page: 10,
        q: this.$route.query.keyword
      });
      this.page++;
      this.loading = false;
      this.list.push(...res.data.data.results);
      // 加载状态结束

      if (this.list.lenght == 0) {
        this.finished = true;
      }

      // 数据全部加载完成
    }
  }
};
</script>

<style lang="less">
.result-contanner {
  height: 100vh;
  padding-top: 46px;
  box-sizing: border-box;
  .search-list {
    height: 100%;
    overflow-y: scroll;
  }
}
</style>
